package com.shop.loan.util;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * request请求处理工具类
 */
@Slf4j
public class RequestUtil {

    private static final String STR_UNKNOWN = "unknown";

    /**
     * 静态取request，本地junit非容器运行时无法使用，服务未启动完成无法使用
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        try {
            ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            return attr.getRequest();
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

//    /**
//     * 获取请求Ip
//     *
//     * @return
//     */
//    public static String getIp() {
//        return getIp(getRequest());
//    }
//
//    /**
//     * 获取ip地址
//     */
//    public static String getIp(HttpServletRequest request) {
//        if (request == null) {
//            return "";
//        }
//        String ip = request.getHeader("X-Forwarded-For");
//
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//                ip = request.getHeader("Proxy-Client-IP");
//            }
//            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//                ip = request.getHeader("WL-Proxy-Client-IP");
//            }
//            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//                ip = request.getHeader("HTTP_CLIENT_IP");
//            }
//            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//            }
//            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//                ip = request.getRemoteAddr();
//            }
//        } else if (ip.length() > 15) {
//            String[] ips = ip.split(",");
//            for (int index = 0; index < ips.length; index++) {
//                String strIp = (String) ips[index];
//                if (!("unknown".equalsIgnoreCase(strIp))) {
//                    ip = strIp;
//                    break;
//                }
//            }
//        }
//        return ip;
//    }


    /**
     * 获取请求头
     *
     * @return
     * @category 获取request请求头
     */
    public static JSONObject getHead() {
        JSONObject jsonObject = new JSONObject();
        HttpServletRequest request = getRequest();
        Enumeration<String> head_enumeration = request.getHeaderNames();
        while (head_enumeration.hasMoreElements()) {
            String key = head_enumeration.nextElement();
            jsonObject.put(key, request.getHeader(key));
        }
        return jsonObject;
    }

    /**
     * 获取平台信息
     *
     * @return
     */
    public static UserAgent getUserAgentInfo() {
        String uaString = getRequest().getHeader("user-agent");
        UserAgent uaInfo = UserAgentUtil.parse(uaString);
        return uaInfo;
    }


}
